package com.liuyfly.leetcode.questions;

import com.liuyfly.leetcode.util.LeetcodeUtil;

/**
 * @author liuyangfang at 2022-08-27 00:45
 */
public class Question0048 {

    public static void rotate(int[][] matrix) {
        LeetcodeUtil.printTwoDimensionalArray(matrix);
        int length = matrix.length;
        for (int i=0;i<length/2;i++) {
            for (int j=0;j<(length+1)/2;j++){
                int temp = matrix[i][j];
                matrix[i][j]=matrix[length-j-1][i];
                matrix[length-j-1][i]=matrix[length-i-1][length-j-1];
                matrix[length-i-1][length-j-1]=matrix[j][length-i-1];
                matrix[j][length-i-1]=temp;
            }
        }
        LeetcodeUtil.printTwoDimensionalArray(matrix);
    }

    public static void rotateMethod1(int[][] matrix) {
        if (matrix.length!=matrix[0].length){
            return;
        }
        int[][] temp = new int[matrix.length][matrix[0].length];
        for (int i=0;i<matrix.length;i++){
            for (int j=0;j<matrix[0].length;j++){
                temp[j][matrix.length-i-1]=matrix[i][j];
            }
        }
        for (int i=0;i<matrix.length;i++) {
            System.arraycopy(temp[i], 0, matrix[i], 0, matrix[0].length);
        }
    }

    public static void main(String[] args) {
        int[][] array = {{11,12,13,14},{15,16,17,18},{19,20,21,22},{23,24,25,26}};
        rotate(array);
    }
}
